//index.js
import polyvLive from '../../utils/polyvlive.js';
import polyvSocket from '../../utils/polyvSocket.js';

const app = getApp();
/*示例用户信息*/
console.log(app)
const liveUid = "d1f406f68f";
const liveVid = "202572";//频道号
Page({

    data: {
        title: "POLYV直播",
        logo: "https://livestatic.videocc.net/assets/wimages/pc_images/logo-default.png",
        companyName: "广州易方信息科技有限公司",
        like: 65,
        view: 500
    },

    onReady() {

        const liveVideoContext = wx.createLivePlayerContext('polyvLiveVideo');

        polyvLive.getVideo({
            uid: liveUid,
            vid: liveVid,
            videoContext: liveVideoContext,
            success: (videoInfo) => {
                // set video src and poster
                this.setPlayerSrc(videoInfo);
            },
            error: (res) => {
                //console.log(res.error);
            },
            onStartLive: () => {
                console.log('start live');
                // set latest video src
                this.setNewPlayerSrc();
            }
        });

    },

    onUnload() {
        polyvLive.destroy();
    },

    // 监听当前直播视频流状态
    onStateChange(e) {
        console.log('stateChnage:', e);
    },

    // 设置播放地址
    // 若使用video组件替代live-player组件，播放地址请使用videoInfo.src
    setPlayerSrc(videoInfo) {
        this.setData({
            video: {
                src: videoInfo.src, // 如果播放组件为video,只能用videoInfo.src(m3u8地址)，如果为live-player则用videoInfo.flvSrc
                poster: videoInfo.poster
            }
        });
    },

    // 设置当前最新播放地址
    setNewPlayerSrc() {
        polyvLive.getNewVideo((videoInfo) => {
            this.setPlayerSrc(videoInfo);
        });
    }



})
